package queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class MainQueue {
	
	private final ArrayBlockingQueue<String> q = new ArrayBlockingQueue<String>(1000);
	
	
    class StringProdercer extends Thread{
    	
    	final String str = "at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453" +
    			"at java.lang.ProcessBuilder.start(ProcessBuilder.java:453  ";
    	
    	@Override
    	public void run(){
    	  System.out.println(Thread.currentThread().getName() + "  Producer messaging...");
    	  q.add(str);
    	}
    }
    
    class Fetcher implements Callable<String>{
    	
    	private int timespan;
    	
    	Fetcher(int timespan){
    		this.timespan = timespan;	
    	}

		public String call() throws Exception {
			String obj = q.take();
            TimeUnit.MILLISECONDS.sleep(timespan);
	        return obj;
        }
    	
    }
    
    public static void main(String[] args){
    	
//    	Thread[] StringProdercer = new Thread[100];
    	ExecutorService es = Executors.newCachedThreadPool();
    	MainQueue m = new MainQueue();
    	Fetcher f = m.new Fetcher(10);
        
    	while(true){
    	  es.execute(m.new StringProdercer());
    	}
    	
    	

    }
	
	
}
